#include "HttpClient.h"
#include "HttpPostRunnable.h"
#include <iostream>

HttpClient::HttpClient()
{
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    if (!curl)
    {
        std::cout << "init fail" << std::endl;
    }
    else
    {
        threadPool.setMaxThreadCount(3);
    }
}
HttpClient::~HttpClient()
{
    curl_easy_cleanup(curl);
}
bool HttpClient::post(const std::string &url, const std::string &data)
{
    HttpPostRunnable *runnable = new HttpPostRunnable(curl);
    runnable->setUrl(url);
    runnable->setData(data);
    runnable->setResponseCallback(std::bind(&HttpClient::response, this, std::placeholders::_1));
    threadPool.start(runnable);

    return true;
}
void HttpClient::response(const std::string &data)
{
    std::cout << data.c_str() << std::endl;
}
